<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible=true">添加分类</el-button>
      </div>
         <el-table :data="cateList" stripe style="width: 100%">
     <el-table-column label="序号" type="index" width="180"></el-table-column>

      <el-table-column prop="cate_name" label="分类名称" width="180"> </el-table-column>

      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>

      <el-table-column label="操作">
          <template v-slot='{row}'>
              <el-button type="primary" size="mini" @click="editCate(row.id)">修改</el-button>
              <el-button type="danger" size="mini" @click="delCate(row.id)">删除</el-button>
          </template>
      </el-table-column>

    </el-table>
    </el-card>

    <el-dialog
        title="添加文章分类"
        :visible.sync="addVisible"
        width="40%"
        @closed='handleAddClose'>

    <el-form :model="addCateForm" ref="addCateRef" :rules="addCateRules"    label-width="80px">
           <el-form-item prop="cate_name" label="分类名称">
               <el-input v-model="addCateForm.cate_name"></el-input>
           </el-form-item>

           <el-form-item prop="cate_alias" label="分类别名" >
               <el-input v-model="addCateForm.cate_alias"></el-input>
           </el-form-item>
     </el-form>
         <span slot="footer" class="dialog-footer">
          <el-button @click="addVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
    </span>
  </el-dialog>

 <el-dialog title="修改文章分类" :visible.sync="editVisible" width="35%" @close="handleEditClose">
   <span>
       <el-form :model="editCateForm" :rules="addCateRules" ref="editFormRef" label-width="100px">
         <el-form-item label="分类名称" prop="cate_name">
         <el-input v-model="editCateForm.cate_name"></el-input>
       </el-form-item>
     <el-form-item label="分类别名" prop="cate_alias">
    <el-input v-model="editCateForm.cate_alias"></el-input>
  </el-form-item>
   </el-form>
  </span>
  <span slot="footer" class="dialog-footer">
    <el-button size="mini" @click="editVisible = false">取 消</el-button>
    <el-button size="mini" type="primary" @click="upDateCate">确 定</el-button>
  </span>
</el-dialog>

  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      // 文章新增弹出框显示与隐藏
      addVisible: false,

      addCateForm: {
        cate_name: '',
        cate_alias: ''
      },
      addCateRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      editVisible: false,
      editCateForm: {}
    }
  },

  methods: {
    async initCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')

      if (res.code === 0) {
        this.cateList = res.data
      }
    },
    handleAddClose () {
      this.$refs.addCateRef.resetFields()
    },

    addCate () {
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return

        const { data: res } = await this.$http.post('/my/cate/add', this.addCateForm)

        if (res.code === 0) {
          this.addVisible = false

          this.initCateList()
        }
      })
    },

    async editCate (id) {
      // id为1或2的数据是不能修改的
      if (id === 1 || id === 2) return this.$message.warning('该数据不能被修改')

      // 发送ajax
      const { data: res } = await this.$http.get('/my/cate/info', { params: { id } })
      if (res.code === 0) {
        this.editCateForm = res.data
        this.editVisible = true
      }
    },
    // 处理编辑弹出框关闭事件
    handleEditClose () {
      this.$refs.editFormRef.resetFields()
    },
    // 更新分类
    upDateCate () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        // axios更新数据
        const { data: res } = await this.$http.put('/my/cate/info', this.editCateForm)
        // 更新失败
        if (res.code !== 0) return this.$message.error(res.message)
        // 更新成功
        this.$message.success(res.message)
        // 关闭弹出框
        this.editVisible = false
        // 重新加载分类数据
        this.initCateList()
      })
    },
    // 根据id删除分类
    async delCate (id) {
      if (id === 1 || id === 2) return this.$message.warning('该数据禁止删除')

      const res = await this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (res !== 'confirm') return

      // axios 根据id删除数据
      const { data: result } = await this.$http.delete('/my/cate/del', {
        params: { id }
      })

      // 判断是否删除成功
      if (result.code !== 0) return this.$message.error(result.message)
      this.$message.success(result.message)
      this.initCateList()
    }

  },

  created () {
    this.initCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
